package com.p24.activities;

import java.util.LinkedList;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleExpandableListAdapter;

import com.p24.R;
import com.p24.XMLParser.XMLMfoParser;
import com.p24.XMLParser.entity.XMLMfoEntity;
import com.p24.http.HttpData;
import com.p24.http.HttpRequest;
import com.p24.info.P24InfoMfo;

public class P24MfoInfo extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.p24mfoinfo);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
		showDialog(0);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		final Dialog dialog = new Dialog(this);
	
		dialog.setContentView(R.layout.p24mfoinfodialog);
		
		Button btnMfoFind = (Button) dialog.findViewById(R.id.btnMfoFind);
		btnMfoFind.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	EditText etBankName = (EditText) dialog.findViewById(R.id.etBankName);
            	EditText etBankMfo = (EditText) dialog.findViewById(R.id.etBankMFO);
            	
            	ListView mfoBankList = (ListView) P24MfoInfo.this.findViewById(R.id.mfoBanksList);
            	ArrayAdapter<String> mfoListAdapter  = new ArrayAdapter<String>(P24MfoInfo.this, android.R.layout.simple_list_item_1);
                mfoBankList.setAdapter(mfoListAdapter);
            	
            	p24InfoMfo = new P24InfoMfo("", "", etBankName.getText().toString(), 
            			etBankMfo.getText().toString());
            	
            	/*try {
                    HttpClient client = new DefaultHttpClient();  
                    String getURL = p24InfoMfo.getURL();
                    HttpGet get = new HttpGet(getURL);
                    HttpResponse responseGet = client.execute(get);
                    HttpEntity resEntityGet = responseGet.getEntity();  
                    if (resEntityGet != null) {
                    	EditText etResult = (EditText) findViewById(R.id.etResult);
                    	etResult.setText(EntityUtils.toString(resEntityGet));
                    }
	            } catch (Exception e) {
	                e.printStackTrace();
	            }*/
	            
	            HttpData httpData = HttpRequest.get(p24InfoMfo.getURL());
	            try {
	            	Log.i("XML", httpData.content);
					XMLMfoParser xmlMfoParser = new XMLMfoParser(httpData.content);
					LinkedList<XMLMfoEntity> mfoList = xmlMfoParser.getMfoInfo();
					for(int i=0; i<mfoList.size(); i++) {
						mfoListAdapter.add(mfoList.get(i).getName());
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
	            
	            //etResult.setText(httpData.content);
            	
            	dialog.cancel();
            }
        });
		
		Button btnMfoCancel = (Button) dialog.findViewById(R.id.btnMfoCancel);
		btnMfoCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	dialog.cancel();
            }
        });
		
		return dialog;
	}
	
	private P24InfoMfo p24InfoMfo = null;
}
